package cn.t215.cloud;

import cn.t215.cloud.api.user.UserDTO;
import cn.t215.cloud.api.user.UserServiceRemoteClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients(basePackages = {"cn.t215.cloud.api"})
public class UserServiceClientApp {
//    @Autowired private RestTemplate restTemplate;
    @Autowired private UserServiceRemoteClient userServiceRemoteClient;

    public static void main(String[] args) {
        SpringApplication.run(UserServiceClientApp.class, args);
    }
   /* @Bean
    @LoadBalanced
    //客户端负载均衡----服务端负载均衡
    RestTemplate restTemplate() {
        return new RestTemplate();
    }*/
   /* @GetMapping("/choose")
    public Object choose() {
        return loadBalancerClient.choose("t215-user-service");
    }*/
//    @GetMapping("")
//    public String callHello() {
//        return restTemplate.getForObject("http://localhost:8001", String.class);
//    }

//    @GetMapping("/hello")
//    public String callHello2() {
//        String object = restTemplate.getForObject("http://t215-user-service", String.class);
//        return object;
//    }

    @GetMapping("/hello3")
    public String callHello3() {
       return userServiceRemoteClient.hello();
    }

    @GetMapping("/callUsers")
    public List<UserDTO> users() {
        return userServiceRemoteClient.users();
    }

    @GetMapping("/callAddUsers")
    public UserDTO saveUser(@RequestBody UserDTO dto) {
        return userServiceRemoteClient.save(dto);
    }
    @GetMapping("/callAddUsers2")
    public UserDTO saveUser2( UserDTO dto) {
        return userServiceRemoteClient.save2(dto);
    }

    @GetMapping("/callSearch")
    UserDTO users(String name, String password) {
        return userServiceRemoteClient.users(name, password);
    }
}
